package com.tgb.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tgb.entity.User;
import com.tgb.entity.UserInfo;
import com.tgb.manager.UserInfoManager;
import com.tgb.manager.UserManager;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource(name="UserManager")
	private UserManager userManager;
	//@Resource(name="UserInfoManager")
	@Autowired
	private UserInfoManager userInfoManager;

	@RequestMapping("/welcome")
	public String welcome(){
		return "welcome";
	}

	@RequestMapping("/getAllUser")
	public String getAllUser(HttpServletRequest request){
		
		request.setAttribute("userList", userManager.getAllUser());
		
		return "index";
	}     
	/*@RequestMapping("/getAllUser")
	public String getAllUser()
	{
		return "index";
	}   */
	
	@RequestMapping("/getUser")
	public String getUser(int id,HttpServletRequest request){
		
		request.setAttribute("user", userManager.getUser(id));
	
		return "editUser";
	}
	
	@RequestMapping("/toAddUser")
	public String toAddUser(){
		return "addUser";
	}
	
	@RequestMapping(value = "/addUser", method= RequestMethod.POST)
	public String addUser(User user,HttpServletRequest request){
		
		userManager.addUser(user);
		request.setAttribute("userList", userManager.getAllUser());
		return "index";
	}
	
	@RequestMapping("/delUser")
	public void delUser(int id,HttpServletResponse response){
		
		String result = "{\"result\":\"error\"}";
		
		if(userManager.delUser(id)){
			result = "{\"result\":\"success\"}";
		}
		
		response.setContentType("application/json");
		
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/updateUser")
	public String updateUser(User user,HttpServletRequest request){
		
		if(userManager.updateUser(user)){
			user = userManager.getUser(user.getId());
			request.setAttribute("userList", userManager.getAllUser());
		//	request.setAttribute("user", user);
			return "index";
		}else{
			return "/error";
		}
	}
	//此处为添加内容
	@RequestMapping("/getAllUserInfo")
	public String getAllUserInfoString(HttpServletRequest request){
		
		request.setAttribute("userInfoList", userInfoManager.getAllUserInfos());
		
		return "list";
	}     
	
	@RequestMapping("/getUserInfo")
	public String getUserInfoString(int id,HttpServletRequest request){
		
		request.setAttribute("userInfo", userInfoManager.getUserInfo(id));
	
		return "editUserInfo";
	}
	
	@RequestMapping("/toAddUserInfo")
	public String toAddUserInString(){
		return "addUserInfo";
	}
	
	@RequestMapping(value = "/addUserInfo", method= RequestMethod.POST)
	public String addUserInfo(UserInfo userInfo,HttpServletRequest request){
		
		userInfoManager.addUserInfo(userInfo);
		request.setAttribute("userInfoList", userInfoManager.getAllUserInfos());
		
	//	return "redirect:/user/getAllUserInfo.html";
		return "list";
	}
	
	@RequestMapping("/delUserInfo")
	public void delUserInfoString(int id,HttpServletResponse response){
		
		String result = "{\"result\":\"error\"}";
		
		if(userInfoManager.delUserInfo(id)){
			result = "{\"result\":\"success\"}";
		}
		
		response.setContentType("application/json");
		
		try {
			PrintWriter out = response.getWriter();
			out.write(result);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/updateUserInfo")
	public String updateUserInString(UserInfo userInfo,HttpServletRequest request){
		
		if(userInfoManager.updateUserInfo(userInfo)){
			userInfo = userInfoManager.getUserInfo(userInfo.getId());
			request.setAttribute("userInfoList", userInfoManager.getAllUserInfos());
		//	request.setAttribute("user", user);
			return "list";
		}else{
			return "/error";
		}
	}
}